<?php
namespace app\api\controller;

use app\api\service\Wxpay;
use think\facade\{View,Session,Db};
use app\BaseController;
use think\exception\HttpResponseException;
class Service extends Common
{
    

    // 查询
    public function createrOrder(){
        $param=input("param.");
        $reaname=$param['reaname'];
        $idcard=$param['idcard'];
        $resaddress=$this->yzcerting($reaname,$idcard);
        //身份验证


        $idath=idcardYas($idcard);
        $data=[];
        //---------
        $xlevelCof=json_decode(Db::name("user_config")->where("id",5)->value("content"),true);
        $abcArr=["A"=>1,"B"=>2,"C"=>3,"D"=>4,"E"=>5];
        $level="A";
        foreach($xlevelCof as $k=>$v){
            if($k!="id"){
                $ast=explode("-",$v);
                if(count($ast)==2){
                    if($ast[0]*1<=$idath["month"]*1 && $ast[1]*1>=$idath["month"]*1){
                        $level=$k;
                    }
                }else{
                    if($v*1==$idath["month"]*1){
                        $level=$k;
                        break;
                    }
                }
            }
        }
        
        $data['xinlevel']=empty($abcArr[$level]) ? "1": $abcArr[$level];//信用等级 1 a 2 b 3 c 4 d 5 e
       //------------------

        //行为分析 - 消费分析--------------
        $xlevelCof=json_decode(Db::name("user_config")->where("id",7)->value("content"),true);
     
        foreach($xlevelCof as $k=>$v){
            if($k!="id"){
                $ast=explode("-",$v);
                if(count($ast)==2){
                    if($ast[0]<=$idath["month"] && $ast[1]>=$idath["month"]){
                        $level=$k;
                    }
                }else{
                    if($v==$idath["month"]){
                        $level=$k;
                        break;
                    }
                }
            }
        }
        $data['x_xiaofei']=[
            "pl"=>trim($level,"b"),
            "xing1"=>1,
            "xing2"=>2,
        ];
        //--------------------------
        //行为分析- 社交风险1-----------------------------------
        $xlevelCof=json_decode(Db::name("user_config")->where("id",6)->value("content"),true);
     
        foreach($xlevelCof as $k=>$v){
            if($k!="id"){
                $ast=explode("-",$v);
                if(count($ast)==2){
                    if($ast[0]<=$idath["month"] && $ast[1]>=$idath["month"]){
                        $level=$k;
                    }
                }else{
                    if($v==$idath["month"]){
                        $level=$k;
                        break;
                    }
                }
            }
        }

        $data['x_sjll1']=[
            "pl"=>trim($level,"b"),
        ];
        //--------------------------------------------
        //行为分析- 社交风险
        $data['x_sjll']=[
            ["key"=>"支付异常行为","value"=>"优"],
            ["key"=>"异常身份信息","value"=>"优"],
            ["key"=>"从事过高风险行业或有关联","value"=>"优"],
            ["key"=>"访问异常网站行为","value"=>"优"],
            ["key"=>"履行合约行为","value"=>"优"],
            ["key"=>"频繁更换住址","value"=>"优"],
            ["key"=>"频繁车辆违章","value"=>"优"],
            ["key"=>"网购行为","value"=>"优"],
            ["key"=>"过渡或异常支出","value"=>"优"],
            ["key"=>"运营商异常名单","value"=>"优"],
            ["key"=>"行政违约行为","value"=>"优"],
            ["key"=>"纳税异常行为","value"=>"优"],
            ["key"=>"与信用风险人群相关联","value"=>"优"],
        ];
        //行为分析-履约行为
        $data['x_lyue1']=[
            "pl"=>$data['xinlevel'], //3,
            "xing1"=>1,
            "xing2"=>2,
        ];
        //行为分析-身份特征-----------------
        $nst=$idath["month"]*1+$idath["day"]*0.01;
        $asy=[];
        foreach(_sht12_sn as $k=>$v){
           if($nst<_sht12_sn[0]["a1"]){
                $asy=_sht12_sn[11];
                $asy["m"]=json_decode(Db::name("user_config")->where("id",2)->value("content"),true)["sgt"][11];
                break;
           }
           if($nst>=_sht12_sn[$k]["a1"] && $nst<=_sht12_sn[$k]["a2"]){
                $asy=$v;
                $asy["m"]=json_decode(Db::name("user_config")->where("id",2)->value("content"),true)["sgt"][$k];
                break;
            }

        }



        

        $data['x_lyue']=[
            ["key"=>"财务估值","value"=>$asy['m']["cfgz"]],
            ["key"=>"年龄特征","value"=>$asy['m']["nltz"]],
            ["key"=>"星座","value"=>$asy['name']],
            ["key"=>"交友类型","value"=>$asy['m']["jylx"]],
            ["key"=>"交友优势","value"=>$asy['m']["jyys"]],
        ];
        //-----------------------
        //基本信息
        $data['basic']=[
            ["key"=>"姓名","value"=>$reaname],
            ["key"=>"年龄","value"=>date("Y")-$idath['year']*1 ],
            ["key"=>"手机号","value"=>Db::name("iuser")->where("id",$this->getUid())->value("mobile")  ],
            ["key"=>"身份证号码","value"=>$idcard],
            ["key"=>"身份证地址","value"=>$resaddress],
            // ["key"=>"手机号所在地","value"=>"山西"],
            ["key"=>"检查结果","value"=>"二要素验证一致"],
        ];
        //报告综述
        $data['repzshu']=[
            ["key"=>"基本信息","value"=>"正常"],
            ["key"=>"二要素验证","value"=>"正常"],
            ["key"=>"申请记录","value"=>"正常"],
            ["key"=>"赊账记录","value"=>"正常"],
            ["key"=>"关联申请记录","value"=>"正常"],
            ["key"=>"法院失信名单","value"=>"正常"],
            ["key"=>"法院执行名单","value"=>"正常"],
            ["key"=>"风险联系人","value"=>"正常"],
            ["key"=>"风险关注名单","value"=>"正常"],
        ];
        //报告详情
        $data['repdetails']=[
            ["key"=>"线上小额申请","value"=>"无"],
            ["key"=>"互联网渠道申请","value"=>"无"],
            ["key"=>"信用办卡平台","value"=>"无"],
            ["key"=>"线下渠道申请","value"=>"无"],
            ["key"=>"线下消费申请","value"=>"无"],
            ["key"=>"汽车消费申请","value"=>"无"],
            ["key"=>"消费赊账申请","value"=>"无"],
            ["key"=>"申请其他的次数","value"=>"无"],
            ["key"=>"大型实体机构申请","value"=>"无"],
            ["key"=>"小额机构申请","value"=>"无"],
        ];
         //报告综述-统计
         $data['repzshu1']=[
            "count1"=>0,
            "count2"=>9,
            "count3"=>0,
        ];//综述报告-详情-评分
        $data['repdetails1']=[
            "xing1"=>1,
            "xing2"=>2,
        ];
        $is=Db::name("order")->insertGetId([
            "sn"=>date("YmdHis").$this->getUid().rand(10000,99999),
            "addtime"=>time(),
            "iuser_id"=>$this->getUid(),
            "price"=> json_decode(Db::name("user_config")->where("id",11)->value("content"),true)["price"],
            "reaname"=>$reaname,
            "idcard"=>$param['idcard'],
            "status"=>0,
            "res_1"=>2,
            "res_msg"=>json_encode($data),
            // "status"=>1
        ]);
        if(trim(input('bd_vid'))){
            $this->doBaiduj(0,$is);
        }
        
        return returnmsg(200,"提交成功",[ "orderId"=> $is ]);
    }
    //订单支付
    public function getorderpay(){
        $uid=$this->getUid();
        $orderId=input("id");
        $res=$this->getwxPay($orderId);
        return returnmsg(200,"操作成功",$res);
    }
    public function getwxPay(){
        $uid=$this->getUid();
        $orderId=input("id");
        $msg=Db::name("order")->where("iuser_id",$uid)->where("status",0)->where("id",$orderId)->find();
        
        if(!$msg){
            
            return returnmsg(199,"该订单已支付");
        }
        
        $link= Db::name("order")->where("id",$orderId)->value("Scheme_link");
         if(!$link){
            $link=$this->setScheme(['order'=>$orderId]);

            Db::name("order")->where("id",$orderId)->update([
                'Scheme_link'=>$link
            ]);
        }
        return returnmsg(200,"订单支付",['orderId'=>$orderId],["link"=>$link]);
            

        
    }
    
    
    
    
    
    public function selectOrder(){
        $page=input("page",1);
        $size = input("size", 10);
         $days=Db::name("wx_basic")->where("id",1)->value("usedays")*1;
 
        $lst=Db::name("order")->where("addtime",">",time()-$days*24*60*60)->where("iuser_id",$this->getUid())->order("id desc")->page($page,$size)->select()->toArray();
        foreach($lst as &$v){
            
            $v['addtime']=date("Y-m-d H:i:s",$v['addtime']);
            $v['res_msg']=json_decode($v['res_msg'],true);
        }
        return returnmsg(200,"检索成功",$lst,[
            "pageCount"=>ceil(Db::name("order")->where("iuser_id",$this->getUid())->count()/$size)
        ]);
    }
    public function orderDetails(){
        $msg=Db::name("order")->where("iuser_id",$this->getUid())->find(input("id"));
        $msg['addtime']=date("Y-m-d H:i:s",$msg['addtime']);
        $msg['res_msg']=json_decode($msg['res_msg'],true);
        return returnmsg(200,"检索成功",$msg);
    }

    public function yzcerting($name='',$idcard=''){
          $host = "https://eid.shumaidata.com";
    $path = "/eid/check";
    $method = "POST";
    $appcode = "2ed7434b30b4403b9b9563a881f0c5a4";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "idcard=$idcard&name=".urlencode($name);
    $bodys = "";
    $url = $host . $path . "?" . $querys;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //设定返回信息中是否包含响应信息头，启用时会将头文件的信息作为数据流输出，true 表示输出信息头, false表示不输出信息头
    //如果需要将字符串转成json，请将 CURLOPT_HEADER 设置成 false
    // curl_setopt($curl, CURLOPT_HEADER, true);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
     $res=curl_exec($curl);
     curl_close($curl);

     $res=json_decode($res,true);
             if($res['code']==0){
            if($res['result']['description']=='一致'){
                return  $res['result']['address'];
            }else{
               

                $re= $res['result']['description'] == "不一致" ? "姓名与身份证号 不一致" : $res['result']['description'];

                throw new HttpResponseException(returnmsg(199,$re));
                return ['code'=>199,"msg"=>$re,"data"=>["description"=>  $re ]];
            }
            
        }else{
            throw new HttpResponseException(returnmsg(199,$res['message']));
            return ['code'=>199,"msg"=>$res['message'],"data"=>["description"=>  $res['message'] ]];
        }

    }

    public function test(){
        $a1=[1,2,3];
        $a2=[2,3,1];
        $a3=[3,2,1];
        $a4=[2,1,1,3,1];
        $a5=[3,3,2,1,2,3,2];
        $a6=[3,1,1,3,2,1,2];
        $a7=[2,2,3,1,3,3,1,2,3,1,1,2];
        $a8=[3,1,3,3,3,1,2];
        $arr=[ $a1,$a2,$a3,$a4,$a5,$a6,$a7,$a8 ];


        //30个

    }



}